﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ezCloud.Data;
using Microsoft.AspNet.Identity;
using ezCloud.Api.Models;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using ezCloud.Api.Common;
using System.Data.SqlTypes;
using System.Collections;
using System.Runtime.Caching;
using CodeSmith.Data.Linq;
using System.Data.Linq;
using System.Reflection;
using System.Globalization;

namespace ezCloud.Api.Controllers
{
    [ApiAuthorize]
    [RoutePrefix("api/Connectivities")]
    public class ConnectivitiesController : ezController
    {
        public ConnectivitiesController()
        {
        }

        public class SmardCardModel
        {
            public int ReservationRoomId { get; set; }
            public string RoomName { get; set; }
            public string GuestName { get; set; }
            public DateTime ArrivalDate { get; set; }
            public DateTime DepartureDate { get; set; }
            public string Reason { get; set; }
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("GetReservationRoom")]
        public async Task<IHttpActionResult> GetReservationRoom(int RRID)
        {
            var user = Common.UserUtitily.getUserProfile();
            var reservationRoom = context.ReservationRooms.Where(rr => rr.HotelId == user.HotelId && rr.ReservationRoomId == RRID).Select(r => new
            {
                room = r,
                roomInfo = r.Rooms,
                roomType = r.RoomTypes,
                traveller = r.Travellers,
                roomPriceName = r.RoomPrices.RoomPriceName,
                reservationNumber = r.Reservations.ReservationNumber
            }).FirstOrDefault();
            return Ok(reservationRoom);
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("GetCardInfomation")]
        public async Task<IHttpActionResult> GetCardInfomation(int RRID,string roomName)
        {
            var user = Common.UserUtitily.getUserProfile();
            var cardInfo = context.SmartCard.Where(sm => sm.HotelId == user.HotelId && sm.ReservationRoomId == RRID && sm.RoomName == roomName).ToList();
            return Ok(cardInfo);
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SaveSmartCard")]
        public async Task<IHttpActionResult> SaveSmartCard(SmardCardModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var user = Common.UserUtitily.getUserProfile();
            SmartCard smartCard = new SmartCard();
            smartCard.HotelId = user.HotelId;
            smartCard.ReservationRoomId = model.ReservationRoomId;
            smartCard.RoomName = model.RoomName;
            smartCard.GuestName = model.GuestName;
            smartCard.ArrivalDate = model.ArrivalDate;
            smartCard.DepartureDate = model.DepartureDate;
            smartCard.CreatedUserId = user.UserId;
            smartCard.CreatedDate = DateTime.UtcNow;
            smartCard.Reason = model.Reason;
            context.SmartCard.InsertOnSubmit(smartCard);
            context.SubmitChanges();
            Elastic.IndexLastAudit(context);
            return Ok();
        }
    }
}